package cn.njyd.corejava.thread.myday02;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Ticket{
    int num = 50;
    public void sell(){
        synchronized (Ticket.class) {
            if (num>0) {
                System.out.println(Thread.currentThread().getName()+"卖出一张票");
                num--;
                System.out.println("目前还剩"+num);
            } else {
                System.out.println(Thread.currentThread().getName()+"票已卖完");
            }
        }
    }
}




public class SynchronizedDemo03 {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        ExecutorService pool = Executors.newFixedThreadPool(4);
        for (int i=1;i<=10;i++) {
            pool.submit(()->{
                for (int j=1;j<=10;j++) {
                    ticket.sell();
                }
            });
        }
        pool.shutdown();

    }


}
